package com.slicktechnologies.client.login;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.simplesoftwares.client.library.appskeleton.AppMemory;
import com.simplesoftwares.client.library.libservice.GenricService;
import com.simplesoftwares.client.library.libservice.GenricServiceAsync;
import com.simplesoftwares.client.library.libservice.LoginService;
import com.simplesoftwares.client.library.libservice.LoginServiceAsync;
import com.slicktechnologies.client.userconfigurations.UserConfiguration;
import com.slicktechnologies.client.utility.AppConnector;
import com.slicktechnologies.client.utility.Screen;
import com.slicktechnologies.client.views.home.HomePresenter;
import com.slicktechnologies.shared.common.helperlayer.LoginProxy;
import com.slicktechnologies.shared.common.helperlayer.User;

public class LoginPresenter implements ClickHandler {
	public static LoginForm login;
	LoginProxy model;
	private final GenricServiceAsync async=GWT.create(GenricService.class);
	private static final LoginServiceAsync loginservice=GWT.create(LoginService.class);
	
	public LoginPresenter(LoginForm view,LoginProxy model)
	{
		login=view;
		this.model=model;
		login.setPresenter(this);
		login.applyHandler(this);
	}

	public LoginForm getLogin() {
		return login;
	}

	public void setLogin(LoginForm login) {
		this.login = login;
	}

	public LoginProxy getModel() {
		return model;
	}

	public void setModel(LoginProxy model) {
		this.model = model;
	}

	@Override
	public void onClick(ClickEvent event) {
		
		getlogin();
			}
	
	public static void showHomeScreen()
	{
		RootPanel rp=RootPanel.get();
		rp.clear();
		AppConnector.initalizes();
		AppMemory mem=AppMemory.getAppMemory();
		mem.initialingapp();
		AppMemory.getAppMemory().setCurrentScreenandChangeProcessName("DashBoard/Home", Screen.HOME);
		HomePresenter.initalize();
		
	}
	
	public static void getlogin()
	{
		LoginProxy proxylogin=login.updateModelFromView();
		loginservice.giveLogin(proxylogin, new AsyncCallback<User>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				caught.printStackTrace();
				
			}

			@Override
			public void onSuccess(User result) {
				if(result==null)
				{
					Window.alert("Invalid Login Credentials!");
					
					;
				}
				else
				{
					UserConfiguration.initUserConfiguration(result,result.getCompanyId());
					System.out.println("Sucess Full----");
					showHomeScreen();
				    
					
				}
				
			}
		});
	}
}







